home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 August / CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso / disc2 / patches / symantec / rtlinc.exe / STRSTREA.H < prev    next >
C/C++ Source or Header  |  1993-06-15  |  8KB  |  221 lines

  1. #ifndef __SSTREAM_H
  2. #define __SSTREAM_H
  3.  
  4. // Iostreams Package
  5. // Bruce Perens, July-August 1990
  6. //
  7. // Modified Steve Teale April 1991
  8. // Copyright Zortech 1990-1991. All Rights Reserved.
  9.  
  10. #include <iostream.h>
  11.  
  12. const int default_allocation = 32;
  13.  
  14. #pragma pack(__DEFALIGN)
  15. class strstreambuf : public streambuf {
  16.  
  17. // This is a streambuf that holds a character array, and I/O is to that
  18. // character array instead of a file or some external character stream.
  19. // There is a dynamic-allocation mode that allocates space for characters
  20. // as needed. Get, put, and seeks are supported within the character array.
  21. // The call freeze() returns a pointer to the array, so that the data may
  22. // be read by conventional string-handling routines.
  23.  
  24. public:
  25.  
  26. // state flags
  27.     enum sstream_flags {
  28.         statmem = 1,
  29. // Set if the buffer was set with an argument to the constructor or
  30. // setbuf, and I should not do dynamic allocation if it runs out of
  31. // space.
  32.  
  33.         frozen = 2,
  34. // Set when the buffer is frozen.
  35.         merged = 4,
  36. // Set if the get and put areas are considered to be overlapped. This
  37. // will be the case if the strstreambuf is dynamic, or if a simultaneous
  38. // seek of both the get and put pointers has been done.
  39.         rdonly = 0x10
  40. // Puts are not allowed.
  41.     };
  42.  
  43.     strstreambuf(int chunksize = default_allocation);
  44. // Create a strstreambuf in dynamic-allocation mode, with the initial
  45. // allocation at least "chunksize" bytes long, defaulted to default
  46. // allocation
  47.  
  48.     strstreambuf(char *memory, int length = 0,
  49.                     char *start_of_put_area = 0);
  50. // Create a strstreambuf using the static buffer at "memory".
  51. // If "length" is positive, that is taken as the length of the
  52. // buffer. If it is zero, the length is taken from strlen(memory).
  53. // If "length" is negative, the buffer is assumed to be of infinite
  54. // length.
  55. //
  56. // The get pointer is initialized to "memory". If "start_of_put_area"
  57. // is zero, the get area covers the entire buffer and a put will be
  58. // considered an error. If "start_of_put_area" is non-zero, the
  59. // get area consists of the bytes between "memory" and
  60. // "start_of_put_area" - 1, and the put area consists of the bytes
  61. // between "start_of_put_area" and the end of the buffer.
  62.  
  63.     strstreambuf(unsigned char *memory, int length = 0,
  64.                     unsigned char *start_of_put_area = 0);
  65. // Same as above, but for an unsigned character buffer.
  66.  
  67.     strstreambuf(void * (*allocate_function)(size_t),
  68.                     void (*free_function)(void *),
  69.                     int chunksize = default_allocation);
  70. // Create a streambuf in dynamic allocation mode. Use
  71. // void * allocate_function(size_t length) to allocate memory,
  72. // and void free_function(void * memory) to free it. Allocation
  73. // chunk size can be specified.
  74.  
  75.     ~strstreambuf();
  76.  
  77.     void freeze(int on = 1);
  78.     void unfreeze() { freeze(0); }
  79. // If the argument is non-zero, "freeze" the strstreambuf. This
  80. // inhibits automatic deletion of the current character buffer.
  81. // This is only important in dynamic-allocation mode. Stores into
  82. // the buffer are invalid when it is "frozen". Calling this with
  83. // a zero argument "un-freezes" the buffer. Deleting a strstreambuf
  84. // will not de-allocate a frozen buffer - you must delete it yourself.
  85.  
  86.     int pcount() const { return pptr()-pbase(); }
  87. // Return the number of characters inserted. Not accurate after
  88. // a seek.
  89.  
  90.     char *str();
  91. // Freeze the buffer and return a pointer to its first byte.
  92. // The pointer may be null if no bytes have been stored and the
  93. // buffer is in dynamic-allocation mode. The buffer may be "un-frozen"
  94. // by calling freeze(0). Deleting a strstreambuf will not de-allocate
  95. // a frozen buffer - you must delete it yourself.
  96.  
  97.     streambuf *setbuf(char *memory, int length);
  98. // The memory argument is not used. The next time the streambuf
  99. // does dynamic allocation, it will allocate at least "length" bytes.
  100. // This function in fact sets the allocation granularity.
  101.  
  102. #if __SC__ > 0x214
  103.     int overflow(int c = EOF);
  104. #else
  105.     int overflow(int c);
  106. #endif
  107.  
  108.     int underflow();
  109.     streampos seekoff(streamoff offset, ios::seek_dir direction,
  110.                     int which =ios::in|ios::out);
  111.     int sync();
  112. // All of these are virtual functions derived from streambuf.
  113. // There's more documentation on them in iostream.h .
  114.  
  115. protected:
  116.     int doallocate();
  117.  
  118. private:
  119.     short sflags;
  120.     int chunk;
  121. // The minimum amount to allocate when doing dynamic allocation.
  122.  
  123.     void *(*allocate_function)(size_t size);
  124. // Points to the function used to allocate memory.
  125.  
  126.     void (*free_function)(void *memory);
  127. // Points to the function used to free memory.
  128.  
  129.     void buffer_setup(char *memory, int length,
  130.                     char *start_of_put_area);
  131. };
  132.  
  133. class istrstream : public istream {
  134.  
  135. // A class of istream that takes as input a character array, and extracts
  136. // information from it.
  137.  
  138. public:
  139.     istrstream(char *memory, int length = 0);
  140. // Create an istrstream attached to the character array at "memory",
  141. // of length "length". If length is zero, strlen() is used, if length
  142. // is negative, the stream is considered to be of a length equivalent
  143. // to the maximum value of type size_t.
  144.  
  145.     ~istrstream();
  146.     strstreambuf *rdbuf() { return &buffer; }
  147.  
  148. private:
  149.     strstreambuf buffer;
  150. };
  151.  
  152. class ostrstream : public ostream {
  153.  
  154. // A class of ostream that inserts information into a character array.
  155.  
  156. public:
  157.     ostrstream();
  158. // Create an ostrstream in dynamic-allocation mode.
  159.  
  160.     ostrstream(char *memory, int length, int mode=ios::out);
  161. // Create an ostrstream attached to the character array at "memory",
  162. // of length "length". If ios::ate or ios::app is set in "mode",
  163. // the buffer is assumed to contain a null-terminated string, and
  164. // the put area begins at the null character. Otherwise the put
  165. // area will begin at "memory".
  166.  
  167.     ~ostrstream();
  168.  
  169.     int pcount() const { return buffer.pcount(); }
  170. // Returns the number of bytes that have been put into the buffer.
  171.  
  172.     char *str();
  173. // Freezes the buffer, and returns a pointer to the first byte of the
  174. // buffer. Once the buffer is frozen it will not be deleted by
  175. // the destructor of the stream: it becomes the user's responsibility
  176. // to delete it.
  177.     void unfreeze();
  178. // Unfreeze the buffer and unconditionally clear the state flags.
  179.  
  180.     strstreambuf *rdbuf() { return &buffer; }
  181. private:
  182.     strstreambuf buffer;
  183. };
  184.  
  185.  
  186. class strstream : public iostream {
  187.  
  188. // A class of iostream that inserts and extracts information in a character
  189. // array.
  190.  
  191. public:
  192.     strstream();
  193. // Create a strstream in dynamic-allocation mode.
  194.  
  195.     strstream(char *memory, int length = 0, int mode = ios::in|ios::out);
  196. // Create a strstream attached to the character array at "memory",
  197. // of length "length". If length is zero, then "memory" is assumed to
  198. // contain a null terminated string, and the langth is taken from
  199. // strlen. If ios::ate or ios::app is set in "mode", the buffer is
  200. // assumed to contain a null-terminated string, and the put area begins
  201. // at the null character. Otherwise the put area will begin at "memory".
  202.  
  203.     ~strstream();
  204.  
  205.     char *str();
  206. // Freezes the buffer, and returns a pointer to the first byte of the
  207. // buffer. Once the buffer is frozen it will not be deleted by
  208. // the destructor of the stream: it becomes the user's responsibility
  209. // to delete it.
  210.     void unfreeze();
  211. // Unfreeze the buffer and unconditionally clear the state flags.
  212.  
  213.     strstreambuf *rdbuf() { return &buffer; }
  214.  
  215. private:
  216.     strstreambuf buffer;
  217. };
  218.  
  219. #pragma pack()
  220. #endif // __SSTREAM_H
  221.